package utils

import (
	"fmt"
	"runtime"
	"syscall"
	"unsafe"
)

var (
	_SetConsoleTitle uintptr
	OS_WIN           bool
	OS_LINUX         bool
)

func init() {
	if runtime.GOOS == "windows" {
		OS_WIN = true
	} else {
		OS_LINUX = true
	}

	if !OS_WIN {
		return
	}

	kernel32, loadErr := syscall.LoadLibrary("kernel32.dll")
	if loadErr != nil {
		fmt.Println("load kernel32.dll err ", loadErr)
		return
	}
	defer syscall.FreeLibrary(kernel32)

	_SetConsoleTitle, _ = syscall.GetProcAddress(kernel32, "SetConsoleTitleW")
}

func SetConsoleTitle(title string) {
	if !OS_WIN {
		return
	}
	_, _, err := syscall.Syscall(_SetConsoleTitle, 1, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), 0, 0)
	if err != 0 {
		fmt.Println("SetConsoleTitle err ", err)
	}
}
